package com.course.web.component.custom;

import com.course.util.DateUtils;
import com.course.web.biz.WebCourseBiz;
import com.course.web.biz.WebSpecBiz;
import com.course.web.component.base.BaseEdit;
import com.course.web.component.base.BaseGrid;
import com.course.web.component.base.BaseList;
import com.course.web.component.base.EditGrid;
import com.course.web.model.CourseInfoVM;
import com.course.web.model.SpecInfoVM;
import com.course.web.view.CoursePlanView;
import com.course.web.view.LearnStatView;
import com.course.web.view.TaskListView;
import com.vaadin.data.Binder;
import com.vaadin.data.converter.StringToIntegerConverter;
import com.vaadin.spring.annotation.SpringComponent;
import com.vaadin.spring.annotation.ViewScope;
import com.vaadin.ui.*;
import com.vaadin.ui.renderers.DateRenderer;
import com.vaadin.ui.renderers.Renderer;

import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;

/**
 * Created by Justin on 2017/6/11.
 */
@SpringComponent
@ViewScope
public class CourseInfoGrid extends EditGrid<CourseInfoVM> {

    private final WebCourseBiz courseBiz;
    private final WebSpecBiz specBiz;

    private BaseList<SpecInfoVM> specList;
    private DateField dateText;

    public CourseInfoGrid(WebCourseBiz courseBiz, WebSpecBiz specBiz) {
        super(CourseInfoVM.class);
        this.courseBiz = courseBiz;
        this.specBiz = specBiz;
    }

    @Override
    protected void doInitMenu(Layout menu) {
        Button create = new Button("添加");
        menu.addComponent(create);
        create.addClickListener(event -> createShow());
    }

    @Override
    protected void doInitGrid(BaseGrid<CourseInfoVM> grid) {
        Renderer renderer = new DateRenderer(new SimpleDateFormat("yyyy-MM-dd"));
        grid.addColumn(CourseInfoVM::getId, "ID");
        grid.addColumn(CourseInfoVM::getTitle, "标题");
        grid.addColumn(CourseInfoVM::getSpecId, "模板");
        grid.addColumn(CourseInfoVM::getWeeks, "周数");
        grid.addColumn(info -> DateUtils.toDate(info.getDate()), "日期").setRenderer(renderer);
        grid.addColumn(CourseInfoVM::getDetail, "介绍");
        grid.addButton(this::updateShow, "编辑");
        grid.addButton(this::task, "任务");
        grid.addButton(this::plan, "排期");
        grid.addButton(this::stat, "统计");
        grid.addButton(this::delete, "删除");
    }

    @Override
    protected void doInitEdit(BaseEdit<CourseInfoVM> edit) {
        HorizontalLayout main = new HorizontalLayout();
        FormLayout form = new FormLayout();
        TextField title = new TextField("标题");
        TextArea detail = new TextArea("介绍");
        TextField weeks = new TextField("周数");
        DateField date = new DateField("上课日期");
        BaseList<SpecInfoVM> specList = new BaseList<>();
        form.addComponent(title);
        form.addComponent(detail);
        form.addComponent(weeks);
        form.addComponent(date);
        form.setCaption("课程信息");
        form.setMargin(false);
        form.setSizeFull();
        specList.setCaption("选择模板");
        specList.setSizeFull();
        main.addComponent(form);
        main.addComponent(specList);
        edit.initEdit(main);
        Binder<CourseInfoVM> binder = edit.binder();
        StringToIntegerConverter converter = new StringToIntegerConverter("输入数字");
        binder.bind(title, "title");
        binder.bind(detail, "detail");
        binder.forField(weeks).withConverter(converter).bind("weeks");
        binder.bind(specList.adapter(), "specId");
        binder.forField(date).bind("date");
        specList.addColumn(SpecInfoVM::getId, "模板ID");
        specList.addColumn(SpecInfoVM::getTitle, "模板标题");
        this.specList = specList;
        this.dateText = date;

        date.addValueChangeListener(event -> {
            LocalDate localDate = event.getValue();
            DayOfWeek dayOfWeek = localDate.getDayOfWeek();
            int weekDay = dayOfWeek.getValue();
            if (weekDay != 1) {
                date.setValue(localDate.minusDays(weekDay - 1));
            }
        });
    }

    @Override
    protected CourseInfoVM doCreate() {
        CourseInfoVM info = new CourseInfoVM();
        LocalDate localDate = LocalDate.now();
        DayOfWeek dayOfWeek = localDate.getDayOfWeek();
        int weekDay = dayOfWeek.getValue();
        if (weekDay != 1) {
            localDate = localDate.minusDays(weekDay - 1);
        }
        info.setDate(localDate);
        return info;
    }

    @Override
    protected void doCreateShow(BaseEdit<CourseInfoVM> edit) {
        dateText.setEnabled(true);
        specList.setEnabled(true);
        super.doCreateShow(edit);
    }

    @Override
    protected void doUpdateShow(BaseEdit<CourseInfoVM> edit) {
        dateText.setEnabled(false);
        specList.setEnabled(false);
        super.doUpdateShow(edit);
    }

    @Override
    protected CourseInfoVM doCreate(CourseInfoVM item) {
        return courseBiz.createInfo(item);
    }

    @Override
    protected CourseInfoVM doUpdate(CourseInfoVM item) {
        return courseBiz.updateInfo(item);
    }

    @Override
    protected CourseInfoVM doDelete(CourseInfoVM item) {
        return courseBiz.deleteInfo(item);
    }

    private void task(CourseInfoVM item) {
        getUI().getNavigator().navigateTo(TaskListView.NAME + "/" + item.getId());
    }

    private void plan(CourseInfoVM item) {
        getUI().getNavigator().navigateTo(CoursePlanView.NAME + "/" + item.getId());
    }

    private void stat(CourseInfoVM item) {
        getUI().getNavigator().navigateTo(LearnStatView.NAME + "/" + item.getId());
    }

    public void enter() {
        init(courseBiz.listInfo());
        specList.setItems(specBiz.listInfoPublished());
    }
}
